package com.wbs.controller;

import cn.hutool.core.util.StrUtil;
import com.wbs.resutil.PageBean;
import com.wbs.resutil.PageResult;
import com.wbs.resutil.Result;
import com.wbs.resutil.ResultEnums;
import com.wbs.service.GoodsService;
import com.wbs.utils.JwtUtil;
import com.wbs.vo.GoodsVO;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: 芝梦
 * @Description: 商品表控制层
 * @DateTime: 2024/1/31 17:59
 */
@RequestMapping("goods")
@RestController
public class GooddsController {
    @Autowired
    private GoodsService goodsService;

    @Autowired
    private JwtUtil jwtUtil;

    @GetMapping("selectByIdGoodsAndDesc")
    public Result selectByIdGoodsAndDesc(@RequestParam Long id){
        GoodsVO goodsVO = goodsService.selectByIdGoodsAndDesc(id);
        if (goodsVO==null){
            return Result.RESPONSE(ResultEnums.THE_ID_DOES_NOT_EXIST);
        }
        return Result.SUCCESS().setData(goodsVO);
    }

    @PostMapping("add")
    public Result add(@RequestHeader String token, @RequestBody GoodsVO goodsVO){
        Claims claims = jwtUtil.parseJwt(token);
        String id = claims.getId();
        goodsVO.setSellerId(id);
        goodsService.add(goodsVO);
        return Result.SUCCESS();
    }

    //根据商家分页查询

    //商品表分页查询
    @PostMapping("findByConditionPage")
    public Result findByConditionPage(@RequestHeader(required = false) String token, @RequestBody PageBean<GoodsVO> pageBean){
        if (!StrUtil.isEmpty(token)){
            Claims claims = jwtUtil.parseJwt(token);
            String id = claims.getId();
            pageBean.getBean().setSellerId(id);
        }
        PageResult<GoodsVO> pageResult = goodsService.findByConditionPage(pageBean);
        return Result.SUCCESS().setData(pageResult);
    }

    //提交审核
    @PutMapping("submitReview")
    public Result submitReview(@RequestParam Long id){
        goodsService.submitReview(id);
        return Result.SUCCESS();
    }

    //审核通过/驳回
    @PutMapping("approvalOrReject")
    public Result approvalOrReject(@RequestParam Long id, @RequestParam String auditStatus){
        goodsService.approvalOrReject(id,auditStatus);
        return Result.SUCCESS();
    }

    //审核通过
//    @PutMapping("update")
//    public Result update(@RequestParam String status, @RequestParam String sellerId){
//        sellerService.update(status,sellerId);
//        return Result.SUCCESS();
//    }
}
